<template>
	<el-dialog v-model="dialogVisible" title="子渠道管理" width="80%" @close="closeDialog">
			<el-card shadow="hover" class="layout-padding-auto">
				<div class="system-user-search mb15">
					<el-input size="default" placeholder="请输入渠道名称" style="max-width: 180px"
						v-model="state.tableData.param.param.cname.val">
					</el-input>
					<el-button size="default" type="primary" @click="seachClick">
						<el-icon>
							<ele-Search />
						</el-icon>
						查询
					</el-button>
					<el-button size="default" type="success" v-auth="'/group/platform/add'"
						@click="onOpenAddCompany('add')">
						<el-icon>
							<ele-FolderAdd />
						</el-icon>
						新增渠道
					</el-button>
				</div>
				<el-table :data="state.tableData.data" v-loading="state.tableData.loading" row-key="id"
					:tree-props="{ children: 'children', hasChildren: 'hasChildren' }" style="width: 100%">
					<el-table-column prop="id" label="ID" show-overflow-tooltip width="80"></el-table-column>
					<el-table-column prop="cname" label="渠道名称" show-overflow-tooltip width="150"></el-table-column>
					<el-table-column prop="off_id" label="URL" show-overflow-tooltip ></el-table-column>
					<el-table-column label="渠道图片" width="120" align="center">
						<template #default="scope">
							<el-image style="width: 80px;height: 40px;background: #000;border-radius: 5px;"
								:src="state.imgUrl + scope.row.img" fit="contain"
								:preview-src-list="[state.imgUrl + scope.row.img]" preview-teleported>
								<template #error>
									<div class="image-slot">
										<el-image style="width: 80px;height: 40px" :src="chatHealth"
											fit="contain"></el-image>
									</div>
								</template>
							</el-image>
						</template>
					</el-table-column>
					<el-table-column label="渠道评级" width="200">
						<template #default="scope">
							<el-rate v-model="scope.row.star_num" disabled text-color="#ff9900" />
						</template>
					</el-table-column>
					<el-table-column prop="status" label="渠道状态" width="120">
						<template #default="scope">
							<el-tag type="success" v-if="scope.row.status == 1">启用</el-tag>
							<el-tag type="info" v-else>禁用</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="创建时间" width="200">
						<template #default="scope">
							<span>{{ dateFormatYMDHMS(scope.row.create_time * 1000) }}</span>
						</template>
					</el-table-column>
					<el-table-column label="操作" width="150">
						<template #default="scope">
							<el-button size="small" text type="primary" v-auth="'/system/plat/conf'"
								@click="onOpenEditConfig(scope.row)">配置</el-button>
							<el-button size="small" text type="primary" v-auth="'/group/platform/edit'"
								@click="onOpenEditCompany('edit', scope.row)">修改</el-button>
							<el-button size="small" text type="primary" v-auth="'/group/platform/del'"
								@click="onRowDel(scope.row)">删除</el-button>
						</template>
					</el-table-column>
				</el-table>
				<el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
					:pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.tableData.param.page"
					background v-model:page-size="state.tableData.param.limit"
					layout="total, sizes, prev, pager, next, jumper" :total="state.tableData.total">
				</el-pagination>
			</el-card>
			<platformDialog ref="platformDialogRef" @refresh="getTableData()" />
			<configDialog ref="configDialogRef" />
	</el-dialog>
</template>

<script setup name="platform">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { platformApi } from '/@/api/platform/index';
import { commApi } from '/@/api/comm/index';
import commonFunction from '/@/utils/commonFunction';
// 定义变量内容
const { dateFormatYMDHMS } = commonFunction();
// 引入组件
const platformDialog = defineAsyncComponent(() => import('./dialog.vue'));
const configDialog = defineAsyncComponent(() => import('./dialogConfig.vue'));
import chatHealth from '/@/assets/baierror.png'
const paramOp = {
	cname: '%%'
}
// 定义变量内容
const platformDialogRef = ref();
const configDialogRef = ref();
const dialogVisible = ref(false);

const state = reactive({
	imgUrl: import.meta.env.VITE_IMG_URL,
	fatherData:null,
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			param: {
				cname: {},
			},
			page: 1,
			limit: 10,
		},
	},
});

const openViewDialog = (row) => {
	state.tableData.param.param.pid = {val: row.id};
	state.fatherData = JSON.parse(JSON.stringify(row));
	dialogVisible.value = true;
	getTableData();
}

// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
	for (let key in state.tableData.param.param) {
		if (state.tableData.param.param[key].val && state.tableData.param.param[key].val !== '') {
			state.tableData.param.param[key].op = paramOp[key] || '='
		} else {
			state.tableData.param.param[key] = {}
		}
	}
	platformApi().index(state.tableData.param).then(res => {
		state.tableData.data = res.data.list;
		state.tableData.total = res.data.total;
		state.tableData.loading = false;
	})
};

// 打开新增渠道弹窗
const onOpenAddCompany = (type) => {
	platformDialogRef.value.openDialog(type,null,state.fatherData);
};

// 打开修改渠道弹窗
const onOpenEditCompany = (type, row) => {
	platformDialogRef.value.openDialog(type, row,state.fatherData);
};

//打开配置
const onOpenEditConfig = (row) => {

	configDialogRef.value.openDialog(row);
}

// 删除渠道
const onRowDel = (row) => {
	ElMessageBox.confirm(`此操作将永久删除渠道名称：“${row.cname}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			platformApi().del({ ids: row.id }).then(res => {
				getTableData();
				ElMessage.success('删除成功');
			})
		})
		.catch(() => { });
};

const seachClick = () => {
	onHandleCurrentChange(1)
}

// 分页改变
const onHandleSizeChange = (val) => {
	state.tableData.param.limit = val;
	getTableData();
};

// 分页改变
const onHandleCurrentChange = (val) => {
	state.tableData.param.page = val;
	getTableData();
};

// 关闭对话框时的处理函数
const closeDialog = () => {
	// 可以在这里添加关闭对话框时的逻辑，比如重置数据等
	// 目前只是简单地不做任何操作
};
// 暴露变量
defineExpose({
	openViewDialog,
});
</script>

<style scoped>
/* 这里可以添加组件的样式 */
</style>